package com.zd.zjfx.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;
import java.util.zip.GZIPInputStream;

import javax.net.ssl.SSLContext;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;

public class Lq_HttpTest {

	public static boolean getDayValue(String path, String encbody) throws Exception {

		try {
			
			HttpHeaders headers = new HttpHeaders();
			headers.add("fwbm", ""); // 添加服务编码
			headers.add("jrdwptbh","1b87dcbcd0ce62ecf90e"); // 添加接入单位平台编号
			headers.add("sydwptbh","1b87dcbcd0ce62ecf90e"); //
										// 添加使用单位平台编号
			headers.add("nlbm",""); // 添加能力编码
			headers.add("ylbm", ""); // 添加用例编码
			headers.add("access_signature", ""); // 添加访问签名
			headers.add("sxcsbz",""); // 添加沙箱测试标志
			String infos [] =path.split("/");
			if(infos[infos.length-3].charAt(0)=='2') {
				headers.add("nlbm",infos[infos.length-3]); // 添加能力编码
				headers.add("ylbm", infos[infos.length-2]); // 添加用例编码
				
				System.out.println(infos[infos.length-2]+infos[infos.length-3]);
			}else {
				headers.add("nlbm",infos[infos.length-2]); // 添加能力编码
				headers.add("ylbm", infos[infos.length-1]); // 添加用例编码
				
				System.out.println(infos[infos.length-1]+infos[infos.length-2]);
			}
			
			String result= null;
													// //接收税局相应报文（未解密）
			GZIPInputStream gzip = null; //商品服务编码返回时为gzip压缩格式
			BufferedReader reader = null;
			StringWriter writer = null;
			try {
				
				TrustStrategy acceptingTrustStrategy = (chain, authType) -> true;
			    SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
			    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
			 
			    HttpClientBuilder clientBuilder = HttpClients.custom();
			 
			    CloseableHttpClient httpClient = clientBuilder.setSSLSocketFactory(sslsf).build();
			 
			    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
			    requestFactory.setHttpClient(httpClient);
			    RestTemplate restTemplate = new RestTemplate(requestFactory);
			    
				HttpEntity<String> entity = new HttpEntity<String>(encbody, headers);
				// 向税局发送请求
				ResponseEntity<byte[]> responseEntity = restTemplate.postForEntity(path, entity, byte[].class);
				// 解析税局返回内容
				List<String> valuesAsList = responseEntity.getHeaders().getValuesAsList("content-encoding");
				// 如果返回 gzip 时处理
				if (!CollectionUtils.isEmpty(valuesAsList) && valuesAsList.contains("gzip")) {
					gzip = new GZIPInputStream(new ByteArrayInputStream(responseEntity.getBody()));
					reader = new BufferedReader(new InputStreamReader(gzip));
					writer = new StringWriter();
					String line;
					while ((line = reader.readLine()) != null) {
						writer.write(line);
					}
					result = writer.toString();
				} else { // 返回非 gzip 时处理
					result = new String(responseEntity.getBody(), StandardCharsets.UTF_8);
				}
			} finally {
				if (Objects.nonNull(gzip)) {
					IOUtils.closeQuietly(gzip);
				}
				if (Objects.nonNull(reader)) {
					IOUtils.closeQuietly(reader);
				}
				if (Objects.nonNull(writer)) {
					IOUtils.closeQuietly(writer);
				}
			}
			

			System.out.println(result);
			//成功处理
		} catch (Exception e) {
			e.printStackTrace();
			//异常处理
		}
		return true;
	}
}
